package com.fanjun.learning.io.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**
 * Created by memedai on 2017/2/14.
 */
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

    @Override
    public void initChannel(SocketChannel socketChannel) throws Exception {
        //LineBaseFrameDecoder换行符解码器
//        socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
        //自定义分隔符解码器
        ByteBuf delimiter = Unpooled.copiedBuffer(TimeServerHandler.separator.getBytes());
        socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,delimiter));
        //自定义固定长度解码器
//        socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(20));
        socketChannel.pipeline().addLast(new StringDecoder());
        socketChannel.pipeline().addLast(new TimeServerHandler());
    }
}
